package site.user.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import modules.recruit.entity.ApplyFinance;
import modules.recruit.form.ApplyFinanceForm;
import modules.recruit.service.ApplyFinanceService;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.guinea.core.service.EntityService;

public class ApplyFinanceActionSite extends ApplyCommonStep {
	
	private ApplyFinanceService applyFinanceService;

	public EntityService getEntityService() {
		return applyFinanceService;
	}

	public ApplyFinanceService getApplyFinanceService() {
		return applyFinanceService;
	}

	public void setApplyFinanceService(ApplyFinanceService applyFinanceService) {
		this.applyFinanceService = applyFinanceService;
	}

	/**
	 * 初始化业务对象的函数.
	 */
	protected void findEntity(ActionForm form, HttpServletRequest request) {
		super.findEntity(form,request);
		if(entity == null && applyBase != null){
			entity = applyBase.getFinance();
		}
		//不是由ID参数得到的entity
		if (id == null && entity != null) {
			ApplyFinance applyFinance = (ApplyFinance)entity;
			ApplyFinanceForm applyFinanceForm = (ApplyFinanceForm) form;
			applyFinanceForm.setId(applyFinance.getId());
			id = applyFinance.getId();
		}
	}

	/**
	 * 显示修改对象Form的Action函数.
	 */
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		bindForm(form, entity);
		ApplyFinanceForm applyFinanceForm = (ApplyFinanceForm)form;
		applyFinanceForm.setApplyBaseId(applyBaseId);
		
		return handleEdit(mapping,form,request,response);
	}
	
	protected void doInitEntity(HttpServletRequest request, ActionForm form) {
		super.doInitEntity(request, form);
		if(isCreateOperation){
			ApplyFinance applyFinance = (ApplyFinance) entity;
			applyFinance.setBase(applyBase);
			entity = applyFinance;
		}
	}


	/**
	 * 保存对象的Action函数.
	 */
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
//		if (!validateForm(mapping, form, request))
//			return handleFormErrors(mapping, form, request, response);
		
		doInitEntity(request, form);

		doSaveEntity(request, form);

		String direction = request.getParameter("direction");
		if (direction != null && direction.equals("next"))
			return new ActionForward(
					"/site/user/applyBg.do?method=edit&applyBaseId="
							+ applyBaseId.toString(), true);
		else if(direction != null && direction.equals("last"))
			return new ActionForward(
					"/site/user/applyEquity.do?method=edit&applyBaseId="
							+ applyBaseId.toString(), true);
		else{
			saveMessage(request, "entity.saved");
			return handleEdit(mapping,form,request,response);
		}
	}
}
